const { STATUS_CODE } = require('../../../constants');
const dbService = require('../../../service/dbService');

const validateReplyComment = async (ctx, next) => {
  const { comment_id, moment_id } = ctx.request.body;
  if (comment_id) {
    const result = await dbService.query({ id: comment_id, moment_id }, 'comment');
    if (result && result.length) {
      await next();
    } else {
      const error = {
        code: STATUS_CODE.FORBIDDEN,
        message: 'forbidden, comment of comment not exist!',
      };
      ctx.app.emit('error', new Error(JSON.stringify(error)), ctx);
      return;
    }
  } else {
    await next();
  }
};

module.exports = validateReplyComment;
